//
//  ICMineNetworkManager.m
//  ICome
//
//  Created by zhangrongwu on 16/9/26.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICLoginNetworkManager.h"
#import "ICHeaderFile.h"


@implementation ICLoginNetworkManager

+(instancetype)sharedInstance {
    static ICLoginNetworkManager *manager;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        manager = [[ICLoginNetworkManager alloc] init];
    });
    return manager;
}


- (NSDictionary *)loginParameters:(NSString *)name
                         password:(NSString *)password
{
    NSString *userAgent = [NSString stringWithFormat:@"%@/%@;%@%.2f;iOS;%@",[ICTools getDeviceTypeInfo],[ICTools getAppVersion],Device,SystemVersion,DeviceType];
    // 设备唯一ID前面拼上设备类型
    NSString *deviceId  = [NSString stringWithFormat:@"%@%@",[ICTools getDeviceTypeInfo],[ICTools getDeviceId]];
    NSDictionary *parameters = @{
                                 @"username" : name,
                                 @"password" : password,
                                 @"userAgent": userAgent,
                                 @"deviceId" : deviceId
                                 };
    return parameters;
}

// 登录
- (void)loginLicensor:(NSString *)name
             password:(NSString *)password
              success:(HttpSuccessCompletionHandle)success
              failure:(HttpFailureCompletionHandle)failure
{
    NSString *url = [self appendRequestBaseURL:@"licensor/login"];
    NSDictionary *parameters = [self loginParameters:name password:password];
    [ICNetworkHelper POST:url parameters:parameters success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

// 得到验证码
- (void)loginGetAuthCode:(NSString *)name
                password:(NSString *)password
                 success:(HttpSuccessCompletionHandle)success
                 failure:(HttpFailureCompletionHandle)failure
{
    NSString *url = [self appendRequestBaseURL:@"licensor/getAuthCode"];
    NSDictionary *parameters = [self loginParameters:name password:password];
    [ICNetworkHelper POST:url parameters:parameters success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

// 登录验证
- (void)loginVerify:(NSString *)name
           password:(NSString *)password
           authCode:(NSString *)autoCode
            success:(HttpSuccessCompletionHandle)success
            failure:(HttpFailureCompletionHandle)failure
{
    NSString *url = [self appendRequestBaseURL:@"licensor/sendAuthCode"];
    NSDictionary *parameters = [self loginParameters:name password:password];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:parameters];
    [dic setObject:autoCode forKey:@"authCode"];
    [ICNetworkHelper POST:url parameters:dic success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}



// 注册zoom
- (void)registerZoomCode:(NSDictionary *)param
                 success:(HttpSuccessCompletionHandle)success
                 failure:(HttpFailureCompletionHandle)failure
{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dict = [userDefaults objectForKey:@"zoomNameP"];
//    if (kStringIsEmpty(dict[@"name"])) {
//        [MBProgressHUD showSuccess:@"用户姓名为空"];
//        return;
//    }
//    
//    if (kStringIsEmpty(dict[@"password"])) {
//        [MBProgressHUD showSuccess:@"用户密码不存在"];
//        return;
//    }
    
    NSString *url = @"http://zoomservericome-icome-prod.ipaas.enncloud.cn:28535/register";
    [ICNetworkHelper POST:url parameters:dict success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
    
}

- (void)logout {
    NSString *userAgent = [NSString stringWithFormat:@"%@/%@;%@%.2f;iOS;%@",[ICTools getDeviceTypeInfo],[ICTools getAppVersion],Device,SystemVersion,DeviceType];
    // 设备唯一ID前面拼上设备类型
    NSString *deviceId  = [NSString stringWithFormat:@"%@%@",[ICTools getDeviceTypeInfo],[ICTools getDeviceId]];
    if ([ICTools stringEmpty:[ICUser currentUser].eId]) {
        return;
    }
    NSDictionary *param = @{@"username":[ICUser currentUser].eId,
                            @"userAgent":userAgent,
                            @"deviceId":deviceId};
    NSString *url = [self appendRequestBaseURL:@"licensor/logout"];
    [ICNetworkHelper POST:url parameters:param success:^(id responseObject) {
        
    } failure:^(NSError *error) {
    }];
}

- (void)getMobile:(NSString *)code
          success:(HttpSuccessCompletionHandle)success
          failure:(HttpFailureCompletionHandle)failure {
    NSDictionary *param = @{@"key":code};
    NSString *url = [self appendRequestBaseURL:@"licensor/getMobile"];
    [ICNetworkHelper POST:url parameters:param success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

- (void)getIdentCode:(NSDictionary *)param
             success:(HttpSuccessCompletionHandle)success
             failure:(HttpFailureCompletionHandle)failure {
    NSString *url = [self appendRequestBaseURL:@"licensor/getIdentCode"];
    [ICNetworkHelper POST:url parameters:param success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

- (void)resetPassword:(NSDictionary *)param
              success:(HttpSuccessCompletionHandle)success
              failure:(HttpFailureCompletionHandle)failure {
    NSString *url = [self appendRequestBaseURL:@"licensor/resetPassword"];
    [ICNetworkHelper POST:url parameters:param success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

- (void)modifyPassword:(NSDictionary *)param
               success:(HttpSuccessCompletionHandle)success
               failure:(HttpFailureCompletionHandle)failure {
    NSString *url = [self appendRequestBaseURL:@"licensor/modifyPassword"];
    [ICNetworkHelper POST:url parameters:param success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

-(void)checkVersionWithParam:(NSDictionary *)param
                     success:(HttpSuccessCompletionHandle)success
                     failure:(HttpFailureCompletionHandle)failure {
    [ICNetworkHelper GET:@"" parameters:param success:^(id responseObject) {
        NSDictionary *response = [responseObject objectForKey:@"responseObject"];
        success(response);
    } failure:^(NSError *error) {
         failure(error);
    }];
}
@end
